package org.deadline.model.config;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.deadline.model.baseapp.BaseEntity;
import org.deadline.model.user.Person;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;

@Entity
public class Industry extends BaseEntity  implements Serializable {

    @Size(min = 3, max = 100)
    private String industryCode;

    @NotNull
    @Size(min = 3, max = 100)
    private String industryName;

    @ManyToOne
    private Person createdBy;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "industry")
    private Set<JobTitle> jobTitles = new HashSet<JobTitle>();

    @Id
	@Column(name="id",unique=true,nullable=false)
	@SequenceGenerator(name="industry_seq_gen",sequenceName="industry_seq")
	@GeneratedValue(generator="industry_seq_gen",strategy=GenerationType.TABLE)
	@Generated(GenerationTime.INSERT)
	private Long id;

    
	public String getIndustryCode() {
        return this.industryCode;
    }

	public void setIndustryCode(String industryCode) {
        this.industryCode = industryCode;
    }

	public String getIndustryName() {
        return this.industryName;
    }

	public void setIndustryName(String industryName) {
        this.industryName = industryName;
    }

	public Person getCreatedBy() {
        return this.createdBy;
    }

	public void setCreatedBy(Person createdBy) {
        this.createdBy = createdBy;
    }

	public Set<JobTitle> getJobTitles() {
        return this.jobTitles;
    }

	public void setJobTitles(Set<JobTitle> jobTitles) {
        this.jobTitles = jobTitles;
    }

	public Long getId() {
	    return this.id;
	}

	public void setId(Long id) {
	    this.id = id;
	}

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	private static final long serialVersionUID = 1L;

	public String toJson() {
        return new JSONSerializer().exclude("*.class").deepSerialize(this);
    }

	public static Industry fromJsonToIndustry(String json) {
        return new JSONDeserializer<Industry>().use(null, Industry.class).deserialize(json);
    }

	public static String toJsonArray(Collection<Industry> collection) {
        return new JSONSerializer().exclude("*.class").deepSerialize(collection);
    }

	public static Collection<Industry> fromJsonArrayToIndustrys(String json) {
        return new JSONDeserializer<List<Industry>>().use(null, ArrayList.class).use("values", Industry.class).deserialize(json);
    }
}
